package com.zyy.smartcity1129.hospital;

import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.JsonObject;
import com.zyy.smartcity1129.BaseFragment;
import com.zyy.smartcity1129.R;
import com.zyy.smartcity1129.data.Department;
import com.zyy.smartcity1129.data.ResultInfo;
import com.zyy.smartcity1129.util.DateStringUtil;
import com.zyy.smartcity1129.util.Http;
import com.zyy.smartcity1129.util.RvAd;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import okhttp3.RequestBody;

/**
 * @ClassName: Department1Fragment$
 * @Description: 作用
 * @Author: letquit
 * @CreateDate: 2/12/2021$
 * @Version: 1.0
 */
public class Department1Fragment extends BaseFragment {
    private RecyclerView rvDepartment1;
    RvAd<Department> rvAd;
    public static String patientName = DepartmentFragment.patientName;
    public static int categoryId = DepartmentFragment.categoryId;
    public static double money = DepartmentFragment.money;
    public static String type = DepartmentFragment.type;
    public static String categoryName = DepartmentFragment.categoryName;
    private TextView tvDepartmentEmp;

    @Override
    protected int layId() {
        return R.layout.f_department1;
    }

    @Override
    protected String title() {
        right.setVisibility(View.VISIBLE);
        right.setText("专家");
        return "普通";
    }

    @Override
    protected boolean back() {
        return true;
    }

    @Override
    protected Boolean bottom() {
        return null;
    }

    @Override
    protected void init(View iv) {
        initView(iv);
        rvDepartment1.setLayoutManager(new LinearLayoutManager(getContext()));
        List<Department> list = new ArrayList<>();
        for (int i = 1; i < 10; i++) {
            Date nextTime = new Date(System.currentTimeMillis() + 1000 * 1800 * i);    //30min
            String sNextTime = DateStringUtil.dateToString(nextTime);
            String ymd = DateStringUtil.stringToYYMMDD(sNextTime);
            String time = DateStringUtil.stringToTime(sNextTime);
            list.add(new Department(categoryId, money, patientName, sNextTime, type, ymd, time));
        }
        initAdapter(list);
        rvDepartment1.setAdapter(rvAd);
        right.setOnClickListener(view -> {
            if (title.getText().toString().equals("普通")) {
                title.setText("专家");
                right.setText("普通");
                tvDepartmentEmp.setVisibility(View.VISIBLE);
                rvDepartment1.setVisibility(View.GONE);
            } else if (title.getText().toString().equals("专家")) {
                title.setText("普通");
                right.setText("专家");
                tvDepartmentEmp.setVisibility(View.GONE);
                rvDepartment1.setVisibility(View.VISIBLE);
            }
        });
    }

    private void initAdapter(List<Department> list) {
        rvAd = new RvAd<Department>(list, R.layout.rv_department) {
            private void initView(View vi) {
                tvRvDepartment1 = (TextView) vi.findViewById(R.id.tvRvDepartment1);
                tvRvDepartment2 = (TextView) vi.findViewById(R.id.tvRvDepartment2);
                tvRvDepartment3 = (TextView) vi.findViewById(R.id.tvRvDepartment3);
                tvRvDepartment4 = (TextView) vi.findViewById(R.id.tvRvDepartment4);
            }

            private TextView tvRvDepartment4;
            private TextView tvRvDepartment3;
            private TextView tvRvDepartment2;
            private TextView tvRvDepartment1;

            @Override
            protected void initR(View vi, Department department, int p) {
                initView(vi);
                Log.i("TAG", department.getReserveTime());
                Log.i("TAG", department.getDate());
                Log.i("TAG", department.getTime());
                String reserveTime = department.getReserveTime();
                String date = department.getDate();
                String week = DateStringUtil.stringToWeek(reserveTime);
                String time = department.getTime();
                String ampm = DateStringUtil.HHToTime(reserveTime);
                tvRvDepartment1.setText(date + " " + week);
                tvRvDepartment2.setText(ampm + " " + time);
                tvRvDepartment3.setText(categoryName);
                tvRvDepartment4.setOnClickListener(view -> {
                    th(() -> {
                        JsonObject jsonObject = new JsonObject();
                        jsonObject.addProperty("categoryId", categoryId);
                        jsonObject.addProperty("money", money);
                        jsonObject.addProperty("patientName", patientName);
                        jsonObject.addProperty("reserveTime", reserveTime);
                        jsonObject.addProperty("type", type);
                        String result = Http.getInstance().get("/prod-api/api/hospital", RequestBody.create(Http.m, jsonObject.toString()), ResultInfo.class, 2).getMsg();
                        ui(() -> {
                            Log.i("TAG", "department " + result);
                            if (result.equals("操作成功")) {
                                Toast.makeText(getContext(), "预约成功！", Toast.LENGTH_SHORT).show();
                                add(new HospitalReserFragment());
                            } else {
                                Toast.makeText(getContext(), result, Toast.LENGTH_SHORT).show();
                            }
                        });
                    });
                });
            }
        };
    }

    private void initView(View iv) {
        rvDepartment1 = (RecyclerView) iv.findViewById(R.id.rvDepartment1);
        tvDepartmentEmp = (TextView) iv.findViewById(R.id.tvDepartmentEmp);
    }
}
